/*
 * @(#)NetDeviceCollector
 *
 * Copyright 2001-2005 友联创新科技
 *
 */
package com.sinodata.bsm.cicp.collector.net;

import java.util.Arrays;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;
import com.sinodata.bsm.common.vo.ResPropertyValue.PropValue;
import com.union.net.collect.AbstractSnmpCollect;
import com.union.net.collect.PropCollectorFactory;
import com.union.net.snmp.SnmpGet;

/**
 * NetDeviceCollector.java
 * <p>
 *
 *
 * @author 李祥军 lixj@uni-mon.com
 * @version 1.0
 * @Date 2010-6-26
 */
public class NetDeviceCollector extends Collector {
    static Logger logger = Logger.getLogger(NetDeviceCollector.class);
    static String[] PropMeta = { "CPU使用率", "内存使用率", "内存总量", "已用内存", "空闲内存" };
    static String[] ParamMeta = { "共同体" };

    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        SnmpGet get = null;
        try {
            ResPropertyValue propertyValueX = new ResPropertyValue();
            PropValue[] propValues = new PropValue[5];
            get = new SnmpGet(ip);
            get.setCommunity(getParamMap().get("共同体"));
            AbstractSnmpCollect collect = PropCollectorFactory.getCollect(get);
            if (collect != null) {
                String[] mem = collect.collectMem(get);
                propValues[0] = ResPropertyValue.createPropValue(mem[2], PropMeta[3]);
                propValues[1] = ResPropertyValue.createPropValue(mem[3], PropMeta[4]);
                propValues[2] = ResPropertyValue.createPropValue(mem[1], PropMeta[2]);
                propValues[3] = ResPropertyValue.createPropValue(mem[0], PropMeta[1]);
                String cpu = collect.collectCpu(get);
                propValues[4] = ResPropertyValue.createPropValue(cpu, PropMeta[0]);
            }

            propertyValueX.setValues(propValues);
            return Arrays.asList(propertyValueX);
        } catch (Exception e) {
            logger.error(e);
            throw new CollectorException(e);
        } finally {
            if (get != null)
                get.close();
        }
    }

    public String[] getParamMeta() {
        return ParamMeta;
    }

    public String[] getPropMeta() {
        return PropMeta;
    }

}
